﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using NUnit.Framework;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;

namespace ERPStore.Offers.Tests
{
	[TestFixture]
	public class OfferTests : ERPStore.Tests.TestBase
	{
		protected ERPStore.Offers.Services.AdminOffersService AdminOffersService { get; private set; }
		protected ERPStore.Services.IOffersService OffersService { get; private set; }
		protected ERPStore.Services.ICatalogService CatalogService { get; private set; }

		[SetUp]
		public void Setup()
		{
			base.Initialize();
			AdminOffersService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Offers.Services.AdminOffersService>();
			CatalogService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.ICatalogService>();
			OffersService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.Services.IOffersService>();
		}

		[Test]
		public void CRUD()
		{
			var offer = AdminOffersService.CreateOffer();

			offer.Name = "OfferTestCRUD";
			offer.Link = "test_crud";
			offer.Description = "This is the best offer in univer now for you";
			offer.EndDate = DateTime.Now.AddDays(10);
			offer.Title = "The offer";

			AdminOffersService.SaveOffer(offer);

			offer.Title = "The offer update";
			offer.ViewName = "View";
			AdminOffersService.SaveOffer(offer);

			offer = AdminOffersService.GetOfferById(offer.Id);

			Assert.AreEqual(offer.Title, "The offer update");
			Assert.AreEqual(offer.ViewName, "View");

			var product = CatalogService.GetProductById(1);

			var offeredProduct = AdminOffersService.CreateOfferedProduct();
			offeredProduct.ProductId = 1;
			offeredProduct.SalePrice = product.SalePrice.Value - 1;
			offeredProduct.OfferId = offer.Id;

			AdminOffersService.SaveOfferedProduct(offeredProduct);

			offer = AdminOffersService.GetOfferById(offer.Id);

			Assert.AreEqual(offer.ProductList.Count, 1);
			offeredProduct = offer.ProductList.First();
			offeredProduct.SalePrice = product.SalePrice.Value - 2;

			AdminOffersService.SaveOfferedProduct(offeredProduct);

			Assert.AreNotEqual(offeredProduct.SalePrice, product.SalePrice);

			product = CatalogService.GetProductById(2);
			offeredProduct = AdminOffersService.CreateOfferedProduct();

			offeredProduct.ProductId = 2;
			offeredProduct.SalePrice = product.SalePrice.Value - 5;
			offeredProduct.OfferId = offer.Id;

			AdminOffersService.SaveOfferedProduct(offeredProduct);

			AdminOffersService.RefreshAllOffers();

			offer = AdminOffersService.GetOfferById(offer.Id);

			Assert.AreEqual(offer.ProductList.Count, 2);

			offeredProduct = offer.ProductList.First();
			AdminOffersService.DeleteOfferedProduct(offeredProduct);
			AdminOffersService.RefreshAllOffers();

			offer = AdminOffersService.GetOfferById(offer.Id);
			Assert.AreEqual(offer.ProductList.Count, 1);

			AdminOffersService.DeleteOffer(offer);

			offer = AdminOffersService.GetOfferById(offer.Id);

			Assert.IsNull(offer);
		}

		[Test]
		public void Validate_Offer()
		{
			var offer = AdminOffersService.CreateOffer();

			var brl = AdminOffersService.ValidateOffer(offer);

			Assert.IsNotEmpty(brl);
		}

		[Test]
		public void Validate_Offer_ReservedName()
		{
			var offer = AdminOffersService.CreateOffer();
			offer.Name = "accueil";

			var brl = AdminOffersService.ValidateOffer(offer);

			Assert.IsNotEmpty(brl);
		}

		[Test]
		public void Apply_Offer_To_Product()
		{
			var offer = AdminOffersService.CreateOffer();

			offer.Name = "OfferTestApply";
			offer.Link = "test_apply";
			offer.Description = "This is the best offer in univer now for you";
			offer.EndDate = DateTime.Now.AddDays(10);
			offer.Title = "The offer";

			AdminOffersService.SaveOffer(offer);

			var product = CatalogService.GetProductById(1);

			var offeredProduct = AdminOffersService.CreateOfferedProduct();
			offeredProduct.ProductId = 1;
			offeredProduct.SalePrice = product.SalePrice.Value - 10;
			offeredProduct.OfferId = offer.Id;

			AdminOffersService.SaveOfferedProduct(offeredProduct);

			OffersService.ApplyOffers(product, null);

			Assert.AreEqual(product.BestPrice.Value, product.SalePrice.Value - 10);
			Assert.IsNotNull(product.SelectedOffer);
			Assert.AreEqual(product.SelectedOffer.Name, offer.Name);
			Assert.IsNull(product.StrikedPrice);
		}

	}
}
